#include "analogComparator.h"



void AnalogComparator::setEnableMultiplexer(unsigned char flag)
{
	SFIOR = SetBit(SFIOR, ACME, flag);
}

void AnalogComparator::setDisable(unsigned char flag)
{
	ACSR = SetBit(ACSR, ACD, flag);
}

void AnalogComparator::setBandgap(unsigned char flag)
{
	ACSR = SetBit(ACSR, ACBG, flag);
}

void AnalogComparator::setInterrupt(unsigned char flag)
{
	ACSR = SetBit(ACSR, ACIE, flag);
}

void AnalogComparator::setInputCapture(unsigned char flag)
{
	ACSR = SetBit(ACSR, ACIC, flag);
}




void AnalogComparator::setInterruptMode(unsigned char flag)
{
	switch(flag)
	{
		case ComparatorInterruptOnOutputToggleAnalogComparator:
			ACSR = SetBit(ACSR, ACIS0, 0);
			ACSR = SetBit(ACSR, ACIS1, 0);
			break;
		case ReservedAnalogComparator:
			ACSR = SetBit(ACSR, ACIS0, 1);
			ACSR = SetBit(ACSR, ACIS1, 0);
			break;
		case ComparatorInterruptOnFallingOutputEdgeAnalogComparator:
			ACSR = SetBit(ACSR, ACIS0, 0);
			ACSR = SetBit(ACSR, ACIS1, 1);
			break;
		case ComparatorInterruptOnRisingOutputEdgeAnalogComparator:
			ACSR = SetBit(ACSR, ACIS0, 1);
			ACSR = SetBit(ACSR, ACIS1, 1);
			break;
		default:
			ACSR = SetBit(ACSR, ACIS0, 0);
			ACSR = SetBit(ACSR, ACIS1, 0);
			break;
	}
}


unsigned char AnalogComparator::checkADC_Off()
{
	if(GetBit(ADCSRA, ADEN)==1)
		return 0;
	else
		return 1;
}


void AnalogComparator::setNegativeComparator(unsigned char flag)
{

	switch(flag)
	{
	case AIN1_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 1);
		break;
	case ADC0_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE0;
		break;
	case ADC1_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE1;
		break;
	case ADC2_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE2;
		break;
	case ADC3_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE3;
		break;
	case ADC4_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE4;
		break;
	case ADC5_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE5;
		break;
	case ADC6_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE6;
		break;
	case ADC7_AnalogComparator:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE7;
		break;
	default:
		SFIOR = SetBit(SFIOR, ACME, 1);
		ADCSRA = SetBit(ADCSRA, ADEN, 0);
		ADMUX = ADMUX & 0xE0;
		break;
	}
}


